package bawei.com.fragment;

import android.os.Bundle;



import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;



import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;



import com.squareup.okhttp.Request;


import com.squareup.okhttp.Response;


import java.util.List;


import bawei.com.adapter.AllareasRankAdapter;
import bawei.com.bilibili.R;



import bawei.com.model.BaseCallback;

import bawei.com.model.Network;
import bawei.com.model.OkHttpHelper;


import bawei.com.model.bean.AllareasRankBean;

/**
 * 类描述:
 * 作者：cwm
 * 时间:2017/3/20 08:52
 * 邮箱:18310832074@163.com
 */

public class AllareasRankFragment extends Fragment {


    private SwipeRefreshLayout swipeRefreshLayout;
    private RecyclerView recyclerView;

    public static AllareasRankFragment newInstance(String type) {

        AllareasRankFragment mFragment = new AllareasRankFragment();
        Bundle mBundle = new Bundle();
        mBundle.putString("type", type);
        mFragment.setArguments(mBundle);
        return mFragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_all_areas_rank,null);
        recyclerView = (RecyclerView) view.findViewById(R.id.recycle);
        swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_refresh_layout);
        return view;
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        String type = getArguments().getString("type");
        getdate(type);
        getRefresh();
    }
    public void  getRefresh(){

        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {

            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        // 停止刷新
                        swipeRefreshLayout.setRefreshing(false);
                    }
                }, 1000); // 2秒后发送消息，停止刷新
            }
        });
        // 设置下拉圆圈上的颜色
        swipeRefreshLayout.setColorSchemeResources(R.color.pink_text_color);
        // 设置手指在屏幕下拉多少距离会触发下拉刷新
        swipeRefreshLayout.setDistanceToTriggerSync(400);
        swipeRefreshLayout.setProgressBackgroundColor(android.R.color.white);
        swipeRefreshLayout.setSize(SwipeRefreshLayout.DEFAULT);
    }
    private void getdate(String s) {

        OkHttpHelper.getinstance().get(Network.ORIGINAL_PH + s, new BaseCallback<AllareasRankBean>() {

            @Override
            public void onRequestBefore() {
            }
            @Override
            public void onFailure(Request request, Exception e) {
            }
            @Override
            public void onSuccess(Response response, AllareasRankBean allareasRankBean) {
                List<AllareasRankBean.RankBean.ListBean> list = allareasRankBean.getRank().getList();
                AllareasRankAdapter adapter=new AllareasRankAdapter(list,getActivity());
                recyclerView.setAdapter(adapter);
                recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
            }

            @Override
            public void onError(Response response, int errorCode, Exception e) {
            }
        });

    }
}
